﻿using System.Threading.Tasks;
#if UNITY_2023_2_OR_NEWER
using UnityEngine;
#endif
using UnityEngine.SceneManagement;

namespace Miao.Unity.SceneManagement
{
    public class StandardScene : GameScene
    {
        public override Scene scene { get; }

        public StandardScene(Scene scene)
        {
            this.scene = scene;
        }

        protected override async Task OnClose()
        {
#if UNITY_2023_2_OR_NEWER
            await Awaitable.FromAsyncOperation(SceneManager.UnloadSceneAsync(scene));
#else
            TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>();
            SceneManager.UnloadSceneAsync(scene).completed += _ => taskCompletionSource.SetResult(true);
            await taskCompletionSource.Task;
#endif
        }
    }
}
